package com.oracle.search.listener;

import com.alibaba.fastjson.JSONObject;
import com.oracle.index.NewsIndex;
import com.oracle.pojo.News;
import com.oracle.search.listener.service.SearchService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
public class EsAddListener implements MessageListener {

    @Autowired
    private SearchService searchService;

    @Override
    public void onMessage(Message message) {
        String json = null;
        try {
            json = new String(message.getBody(),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //索引数据到ES
        News news = JSONObject.parseObject(json,News.class);
        this.searchService.indexNews(news);
    }
}
